package com.oinux.playhelper.util;

import java.util.Calendar;
import java.util.Date;

public class TimeUtils {
	public static final long DIVIDE_TO_MINUTES = 60 * 1000;
	public static final long DIVIDE_TO_HOUR = 60 * DIVIDE_TO_MINUTES;
	public static final long DIVIDE_TO_DAY = 24 * DIVIDE_TO_HOUR;
	public static final long DIVIDE_TO_MONTH = 30 * DIVIDE_TO_DAY;
	public static final long DIVIDE_TO_YEAR = 12 * DIVIDE_TO_MONTH;

	public static String formatTime(long time) {
		boolean up = false;
		StringBuilder builder = new StringBuilder("约");
		if (time >= DIVIDE_TO_YEAR) {
			builder.append(time / DIVIDE_TO_YEAR + "年");
			time = time % DIVIDE_TO_YEAR;
			up = true;
		}
		if (time >= DIVIDE_TO_MONTH) {
			builder.append(time / DIVIDE_TO_MONTH + "月");
			time = time % DIVIDE_TO_MONTH;
		} else if (up) {
			builder.append("0月");
			up = false;
		}
		if (time >= DIVIDE_TO_DAY) {
			builder.append(time / DIVIDE_TO_DAY + "天");
			time = time % DIVIDE_TO_DAY;
		} else if (up) {
			builder.append("0天");
			up = false;
		}
		if (time >= DIVIDE_TO_HOUR) {
			builder.append(time / DIVIDE_TO_HOUR + "小时");
			time = time % DIVIDE_TO_HOUR;
		} else if (up) {
			builder.append("0小时");
			up = false;
		}
		builder.append(time / DIVIDE_TO_MINUTES + "分钟");
		return builder.toString();
	}

	public static Date getDateBefore(Date date, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
		return now.getTime();
	}
	
	public static Date getDateAfter(Date date, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		return now.getTime();
	}
	
	public static Date getTodayZero() {
		Calendar now = Calendar.getInstance();
		now.set(Calendar.HOUR_OF_DAY, 0);
		now.set(Calendar.MINUTE, 0);
		now.set(Calendar.SECOND, 0);
		return now.getTime();
	}
}
